package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.impl.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/account")
@SessionAttributes({"accountList","account"})
public class ManageController {
     @Autowired
     private AccountService accountService;


     @GetMapping("/main")
     public String viewallaccount(Model model){
         List<Account> accountList=accountService.getAllAccount();
         model.addAttribute("accountList",accountList);
         return "Account/account_info";
     }
     @GetMapping("/updateaccount")
     public String updateaccount(String username,Model model){
         System.out.println("想要编辑的用户是："+username);
         Account account=accountService.getAccount(username);
         model.addAttribute("account",account);
         return "Account/account_mdf";
     }
     @PostMapping("/updateInformation")
    public String updateaccount(HttpServletRequest req, Account account, Model model, HttpSession session){
         try {
             req.setCharacterEncoding("UTF-8");
         } catch (Exception e) {

         }
         String username = req.getParameter("username");
         String password = req.getParameter("password");
         System.out.println("获取的密码是："+password);
         String firstName = req.getParameter("firstname");
         String lastName = req.getParameter("lastname");
         String email = req.getParameter("email");
         String phone = req.getParameter("phone");
         String address1 = req.getParameter("address1");
         String address2 = req.getParameter("address2");
         String city = req.getParameter("city");
         String state = req.getParameter("state");
         String zip = req.getParameter("zip");
         String country = req.getParameter("country");
         String languagePreference = req.getParameter("languagePreference");
         String favouriteCategoryId = req.getParameter("favouriteCategoryId");
//        String listOption = req.getParameter("listOption");
//        String bannerOption = req.getParameter("bannerOption");
         String a = req.getParameter("listOption");
         boolean listOption =false;
         if(a!=null&&a.equals("1"))
         {
             listOption= true;
         }
         String b = req.getParameter("bannerOption");
         boolean bannerOption = false;
         if(b!=null&&b.equals("1"))
         {
             bannerOption= true;
         }
         account.setUsername(username);
         account.setPassword(password);
         account.setFirstName(firstName);
         account.setLastName(lastName);
         account.setEmail(email);
         account.setPhone(phone);
         account.setAddress1(address1);
         account.setAddress2(address2);
         account.setCity(city);
         account.setState(state);
         account.setZip(zip);
         account.setCountry(country);
         account.setLanguagePreference(languagePreference);
         account.setFavouriteCategoryId(favouriteCategoryId);
         account.setListOption(listOption);
         account.setBannerOption(bannerOption);
         accountService.updateAccount(account);
         model.addAttribute("account", account);
         model.addAttribute("msg", "  修改个人信息成功");
         //到时用js的alert方法提示修改成功
         return "Account/account_mdf";
     }
     @RequestMapping("/searchAccount")
     public String search(Model model,String keyword){
          System.out.println("搜索的关键字是："+keyword);
          List<Account> accountList=accountService.searchAccount(keyword);
          model.addAttribute("accountList",accountList);
          return "Account/account_info";
     }
//     @GetMapping("/resetpassword")
//    public String resetPassword(){
//         return "account/ResetPassword";
//     }
//     @GetMapping("/usernameExist")
//     @ResponseBody
//    public String usernameexist(Model model,String username){
//         System.out.println("修改密码的用户是:"+username);
//         Account result=accountService.getAccount(username);
//         System.out.println("查询数据库的结果是："+result);
//         if(result==null){
//             return "Not Exist";
//         }else {
//             return "Exist";
//         }
//
//     }
//     @PostMapping("/submitnewpassword")
//    public String submitnewpassword(Model model,String username,String password,String newpassword){
//         //先获取要修改密码的用户
//         System.out.println("修改的用户名是："+username);
//         System.out.println("旧密码是："+password);
//         System.out.println("新的密码是："+newpassword);
//         Account account=accountService.getAccount(username);
//         //重置密码
//         account.setPassword(newpassword);
//         accountService.updatesignon(account);
//         model.addAttribute("account",account);
//         return "account/updatesuccess";
//     }
     @GetMapping("/deleteaccount")
    public String deleteaccount(String username,Model model){
         //先获取删除的用户名
         System.out.println("要删除的用户名："+username);
         //业务逻辑
         accountService.deleteaccount(username);
         //删除过后的新所有用户表
         List<Account> accountList= accountService.getAllAccount();
         model.addAttribute("accountList",accountList);
         return "Account/account_info";
     }


}
